<template>
  <div class="register-container">
    <div class="register-card">
      <h2 class="register-title">员工注册</h2>
      
      <el-form 
        :model="form"
        @submit.prevent="onSubmit" 
        class="register-form"
        label-position="top"
      >
        <el-form-item label="姓名" prop="name">
          <el-input 
            v-model="form.name" 
            placeholder="请输入真实姓名"
            prefix-icon="User"
            clearable 
          />
        </el-form-item>
 
        <el-form-item label="身份证号" prop="cardid">
          <el-input 
            v-model="form.cardid" 
            placeholder="请输入18位身份证号"
            maxlength="18"
            show-word-limit 
            clearable 
          />
        </el-form-item>
 
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="form.phone" 
            placeholder="请输入11位手机号"
            maxlength="11"
            prefix-icon="Iphone"
            clearable
          />
        </el-form-item>
 
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password" 
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
 
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            prefix-icon="Lock"
            show-password 
          />
        </el-form-item>
 
        <el-button
          native-type="submit"
          type="primary"
          class="register-btn"
          :loading="loading"
        >
          立即注册 
        </el-button>
      </el-form>
 
      <div class="register-footer">
        <router-link to="/elogin" class="footer-link">
          已有工号？立即登录 
        </router-link>
      </div>
    </div>
  </div>
</template>
 
<script>
import { employee_registerAPI } from '@/api/api.js'; 
import { ElMessage, ElMessageBox } from 'element-plus';
import { User, Lock, Iphone } from '@element-plus/icons-vue';
 
export default {
  data() {
    return {
      form: {
        name: "",
        cardid: "",
        phone: "",
        password: ""
      },
      confirmPassword: "",
      loading: false
    };
  },
  components: { User, Lock, Iphone },
  methods: {
    async onSubmit() {
      // 基础验证
      // if (!this.form.name.trim())  {
      //   ElMessage.warning(" 请输入姓名");
      //   return;
      // }
      // if (!/^\d{17}[\dXx]$/.test(this.form.cardid))  {
      //   ElMessage.warning(" 请输入有效的18位身份证号");
      //   return;
      // }
      // if (!/^1[3-9]\d{9}$/.test(this.form.phone))  {
      //   ElMessage.warning(" 请输入有效的手机号");
      //   return;
      // }
      // if (this.form.password.length  < 4) {
      //   ElMessage.warning(" 密码长度不能少于4位");
      //   return;
      // }
      if (this.form.password  !== this.confirmPassword)  {
        ElMessage.error(" 两次密码不一致");
        return;
      }
 
      try {
        this.loading  = true;
        const res = await employee_registerAPI(this.form); 
        
        if (res.code  === 200) {
          ElMessageBox.alert( 
            `注册成功！您的工号ID是: ${res.data}`, 
            '注册成功',
            {
              confirmButtonText: '确定',
              callback: () => {
                this.$router.push('/elogin'); 
              }
            }
          );
        } else {
          ElMessage.error(res.message  || "注册失败");
        }
      } catch (error) {
        console.error(' 注册错误:', error);
        const errorMsg = error.response?.data?.message  || 
                        error.message  || 
                        '注册失败，请稍后重试';
        ElMessage.error(errorMsg); 
      } finally {
        this.loading  = false;
      }
    }
  }
};
</script>
 
<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}
 
.register-card {
  width: 420px;
  padding: 40px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}
 
.register-card:hover {
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.32);
}
 
.register-title {
  text-align: center;
  margin-bottom: 30px;
  color: #2c3e50;
  font-size: 24px;
  font-weight: 500;
}
 
.register-form {
  margin-top: 20px;
}
 
:deep(.el-form-item__label) {
  font-weight: 500;
  color: #5a5e66;
  margin-bottom: 8px;
}
 
.register-btn {
  width: 100%;
  height: 44px;
  margin-top: 10px;
  font-size: 16px;
  letter-spacing: 2px;
  background-color: rgba(0, 203, 230, 0.775);
  border: none;
  transition: all 0.3s;
}
 
.register-btn:hover {
  background-color: rgba(0, 221, 255, 0.618);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(230, 115, 0, 0.3);
}
 
.register-footer {
  margin-top: 25px;
  text-align: center;
  font-size: 14px;
}
 
.footer-link {
  color: #606266;
  text-decoration: none;
  transition: color 0.3s;
}

/* 修改聚焦时的阴影 */
:deep(.el-input__wrapper.is-focus)  {
  box-shadow: 0 0 0 1px rgba(100, 99, 99, 0.785) inset !important;
}
 
/* 修改hover状态 */
:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px rgba(139, 137, 136, 0.614) inset !important;
} 
 
 
.footer-link:hover {
  color: rgba(0, 199, 230, 0.842);
}
</style>